package com.emc.atmos.test;

import java.io.InputStream;
import java.util.Properties;

import com.emc.atmos.test.AtmosExMetaDBConnector;

import org.apache.log4j.Logger;

public class AtmosExMetaGateway {

	private byte [] tagByteArray;
	private String oid = null;
	private Properties props = new Properties();

	private static Logger log = Logger.getLogger(AtmosJig.class);

	public AtmosExMetaGateway()
	{
		
	}
	public AtmosExMetaGateway(String local_oid, byte [] local_tagByteArray)
	{
		this.oid = local_oid;
		this.tagByteArray = local_tagByteArray;
	}
	public int AtmosExMetaGatewayPut()
	{
		int status=0;
		
		String [] targets = getTargets();
		
		for (String target : targets)
		{
			//log.info("in exmetaPUT cur target is " + target);
			
			String curTargetType = targetType(target); 
			
			//log.info("in exmetaPUT cur target type is " + curTargetType);

			if (curTargetType.equals("DB"))
			{
				String [] curTargetInfo = targetInfo(target,curTargetType);
				String curString = new String(this.tagByteArray);
				curString = curString.replaceAll("=", ",");
				String [] curStringAr = curString.split(",");
				
				AtmosExMetaDBConnector myDBconnector = new AtmosExMetaDBConnector(this.oid,curTargetInfo[0],curTargetInfo[1],curTargetInfo[2],curStringAr,curTargetInfo[3]);
				
				status = myDBconnector.UploadExMeta();
				
				
				
			}
		}
		
		return status;
	}
	public byte [] AtmosExMetaGatewaySearch(String searchString)
	{
		byte [] result = null;
		
		return result;
	}
	
	private void loadProps()
	{
		try{
			InputStream propfile = this.getClass().getClassLoader().getResourceAsStream("targets.properties");
			
			try{
				this.props.load(propfile);
			} catch (Exception e){
				this.props.load(propfile);
			}
			propfile.close();
		} catch (Exception e){
			log.error("error loading properties for worker.");
			log.error(e.getMessage());
		}
	}
	
	private String [] getTargets()
	{
		String [] putTargets = null;
		String tempTargets = null;
		
		this.loadProps();
		
		tempTargets = this.props.getProperty("targets");
		
		tempTargets = tempTargets.replaceAll("\\s+", "");
		putTargets=tempTargets.split(",");
				
		return putTargets;
		
	}
	private String targetType(String localTarget)
	{
		String returnType = null;
		
		//if (localTarget.equals("GP"))
		//{
			String tempstring = localTarget + "targetType";
			returnType = this.props.getProperty(tempstring);
		//}
		
		return returnType;
	}
	private String [] targetInfo (String local_target, String local_targetType)
	{
		String [] returnStringAr = null;
		
		if (local_targetType.equals("DB"))
		{
			String [] tRetAr = new String[4];
			
			String tempstring = local_target + local_targetType + "Url";
			tRetAr[0] = this.props.getProperty(tempstring);
			tempstring = local_target + local_targetType + "Username";
			tRetAr[1] = this.props.getProperty(tempstring);
			tempstring = local_target + local_targetType + "Password";
			tRetAr[2] = this.props.getProperty(tempstring);
			tempstring = local_target + local_targetType + "Drivername";
			tRetAr[3] = this.props.getProperty(tempstring);
			
			returnStringAr = tRetAr;
		}
		
		return returnStringAr;
		
	}
}
